#define _CRT_SECURE_NO_WARNINGS 1
struct ListNode* reverseKGroup(struct ListNode* head, int k)
{
    if (head == NULL)
        return NULL;
    if (k == 1)
        return head;
    struct ListNode** cur = (struct ListNode**)malloc(k * sizeof(struct ListNode*));
    cur[0] = head;
    int i = 0;
    for (i = 1; i < k; i++)
    {
        cur[i] = cur[i - 1]->next;
        if (cur[i] == NULL)
        {
            k = i;
            break;
        }
    }
    cur[0]->next = cur[k - 1]->next;
    for (i = 1; i < k; i++)
    {
        cur[i]->next = cur[i - 1];
    }
    head = cur[k - 1];
    struct ListNode* prev = NULL;
    while (cur[0]->next)
    {
        prev = cur[0];
        cur[0] = cur[0]->next;
        for (i = 1; i < k; i++)
        {
            cur[i] = cur[i - 1]->next;
            if (cur[i - 1]->next == NULL)
                goto label;
        }
        prev->next = cur[k - 1];
        cur[0]->next = cur[k - 1]->next;
        for (i = 1; i < k; i++)
        {
            cur[i]->next = cur[i - 1];
        }
    }
label:;
    return head;
}

